package com.base.git;

import org.eclipse.jgit.api.CommitCommand;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;

import java.io.File;
import java.io.IOException;
import java.util.List;

public class GitCommitDemo {

    public static void main(String[] args) throws Exception {
        // 指定本地仓库目录路径
        String localRepositoryPath = "D:\\java\\work\\base-demo";

        // 打开本地仓库
        Repository repository = new FileRepositoryBuilder()
                .setGitDir(new File(localRepositoryPath + "/.git"))
                .build();

        // 创建 Git 对象
        Git git = new Git(repository);

        // 添加要提交的文件
        // git.add().addFilepattern(".").call();

        // 创建提交命令
        CommitCommand commitCommand = git.commit();
        // 设置作者和提交消息
        commitCommand.setAuthor("Your Name", "your_email@example.com");
        commitCommand.setMessage("test commit");

        // 提交更改
        commitCommand.call();

        System.out.println("Committed changes");

        // 关闭 Git 和 Repository 对象
        git.close();
        repository.close();
    }
}